﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ESXPowerTools.Controller.Enums;

namespace ESXPowerTools.UIApp
{
    public partial class MainForm : Form
    {
        #region Properties

        InitLogoForm initForm = new InitLogoForm();

        #endregion

        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(InitViewManager);
            t.Start();
            initForm.Show();

            CRLable.Text += System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

            VMStateBox.Items.Clear();
            VMStateBox.Items.Add(CVMState.Pause);
            VMStateBox.Items.Add(CVMState.PowerOFF);
            VMStateBox.Items.Add(CVMState.PowerON);
            VMStateBox.Items.Add(CVMState.Resume);
            VMStateBox.Items.Add(CVMState.Snapshot);




        }

        public void InitViewManager()
        {
            this.Opacity = 0;
            Thread.Sleep(2000);
            initForm.Hide();
            this.Opacity = 100;
        }

        private void ConnectBTN_Click(object sender, EventArgs e)
        {
            ConnectBTN.Enabled = false;
            Thread backLogin = new Thread(DoLogin);
            backLogin.Start();
        }

        private void DoLogin(object obj)
        {
            try
            {
                Program.ControllerRef.OpenConnection(HostTextBox.Text, UserTextBox.Text, PassBox.Text);
                ExceptionTextBox.Invoke(new Action(() => ExceptionTextBox.Text = string.Empty));
                ExceptionTextBox.Invoke(new Action(() => LoginStatus.Text = "OK!"));
                ExceptionTextBox.Invoke(new Action(() => ManageVMS.Enabled = true));


                Thread rowsDraw = new Thread(VMSGridFiller);
                rowsDraw.Start();




            }
            catch (Exception ex)
            {
                ExceptionTextBox.Invoke(new Action(() => ExceptionTextBox.Text = ex.ToString()));
                ExceptionTextBox.Invoke(new Action(() => ConnectBTN.Enabled = true));
            }
        }

        private void ManageVMS_Click(object sender, EventArgs e)
        {

        }


        public void VMSGridFiller()
        {
            foreach (var item in Program.ControllerRef.GetVMList(false))
	        {
		        DataGridViewRow r = new DataGridViewRow();

                VMSGrid.Rows.Add(r);
	        }

        }
    }
}
